from django.views import View

from Venue.models import VenueSport, VenueSportYard
from sportsground.utils import Req
from sportsground.utils.Res import Res
from sportsground.utils.views import LoginRequired


class YardView(View):
    """ 场地运动类型信息 """

    def get(self, request):
        """ 查询 """
        # venue = request.user.venue
        # sports = venue.venuesport_set.all()
        yards = VenueSportYard.objects.all()
        result = []
        for yard in yards:
            result.append(yard.Info())
        return Res.Json(Res.OK, result=result)

    @LoginRequired.Venue
    @Req.getJson
    def post(self, request):
        """ 添加 """
        name = request.json_dict.get("name")
        type = request.json_dict.get("type")
        message = request.json_dict.get("message")
        readme = request.json_dict.get("readme")
        sportId = request.json_dict.get("sportId")

        venue = request.user.venue
        if type not in VenueSportYard.Type.list():
            return Res.Json(Res.ParamErr)
        sport = VenueSport.objects.filter(venue=venue, id=sportId).first()
        if not sport:
            return Res.Json(Res.Warn, msg="无效的sportId")

        yard = VenueSportYard.create(sport, type=type, name=name, readme=readme, message=message)
        return Res.Json(Res.OK, result=yard.Info())

    @LoginRequired.Venue
    @Req.getJson
    def put(self, request):
        """ 修改 """
        id = request.json_dict.get("id")
        name = request.json_dict.get("name")
        type = request.json_dict.get("type")
        message = request.json_dict.get("message")
        readme = request.json_dict.get("readme")
        sportId = request.json_dict.get("sportId")

        venue = request.user.venue

        yard = VenueSportYard.objects.filter(id=id).first()
        if not yard:
            return Res.Json(Res.ParamErr)

        if name:
            yard.name = name
        if type:
            yard.type = type
        if message:
            yard.message = message
        if readme:
            yard.readme = readme
        if sportId:
            sport = VenueSport.objects.filter(id=sportId).first()
            yard.venueSport = sport

        yard.save()

        return Res.Json(Res.OK, result=yard.Info())

    @LoginRequired.Venue
    @Req.getJson
    def patch(self, request):
        """ 修改时间表 """
        id = request.json_dict.get("id")
        index = request.json_dict.get("index")
        times = request.json_dict.get("times", [])
        default = request.json_dict.get("default")

        venue = request.user.venue
        yard = VenueSportYard.objects.filter(id=id).first()
        if not yard:
            return Res.Json(Res.ParamErr)

        if default:
            yard.default = default

        if index != None:
            t = yard.time
            t[index] = times
            yard.time = t

        yard.save()
        return Res.Json(Res.OK, result=yard.Info())

    @LoginRequired.Venue
    @Req.getJson
    def delete(self, request):
        """ 删除 """
        id = request.json_dict.get("id")

        yard = VenueSportYard.objects.filter(id=id).first()
        if not yard:
            return Res.Json(Res.ParamErr)

        yard.delete()
        return Res.Json(Res.OK)
